/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

package org.ponly.cta4j.factory.gimpy;

import com.jhlabs.image.SwimFilter;
import com.jhlabs.math.ImageFunction2D;
import java.awt.Color;
import java.awt.Font;
import org.ponly.cta4j.CaptchaFactory;
import org.ponly.cta4j.drawing.FilterablePainter;
import org.ponly.cta4j.drawing.Painter;
import org.ponly.cta4j.drawing.background.BackgroundBrush;
import org.ponly.cta4j.drawing.background.ColorBackgroundBrush;
import org.ponly.cta4j.drawing.color.RandomColorGenerator;
import org.ponly.cta4j.drawing.filter.BufferedImageOpFilter;
import org.ponly.cta4j.drawing.filter.Filter;
import org.ponly.cta4j.drawing.font.FontFamily;
import org.ponly.cta4j.drawing.font.RandomFontFamily;
import org.ponly.cta4j.drawing.text.GlyphsBrush;
import org.ponly.cta4j.drawing.text.TextBrush;
import org.ponly.cta4j.drawing.text.glyphsdecorator.GlyphsDecorator;
import org.ponly.cta4j.drawing.text.glyphsvisitor.*;
import org.ponly.cta4j.factory.ProxiedCaptchaFactory;

/**
 * 简单风格的验证码工厂, 来自于网络 jcaptcha HotmailEngine.
 *
 * @author vacoor
 * @since 1.0
 */
public class DefaultFactory extends ProxiedCaptchaFactory {
    /**
     * 默认实例.
     */
    public static final DefaultFactory INSTANCE = new DefaultFactory();

    /*
     * captcha width.
     */
    private static final int WIDTH = 100;

    /**
     * captcha height.
     */
    private static final int HEIGHT = 38;

    private GimpyFactory factory = createFactory();

    /**
     * {@inheritDoc}
     */
    @Override
    protected CaptchaFactory getCaptchaFactory() {
        return factory;
    }

    /**
     * 创建一个 HOTMAIL 风格的验证码工厂.
     *
     * @return hotmail style 验证码工程
     */
    protected GimpyFactory createFactory() {
        final SwimFilter swim = new SwimFilter();
        swim.setScale(30);
        swim.setStretch(1);
        swim.setTurbulence(1);
        swim.setAmount(2);
        swim.setTime(0);
        swim.setEdgeAction(ImageFunction2D.CLAMP);

        final Filter textFilter = BufferedImageOpFilter.create(swim);
        final BackgroundBrush backgroundBrush = new ColorBackgroundBrush();
        final TextBrush textBrush = new GlyphsBrush(4, 4, new RandomColorGenerator(
//                new Color(0x306DB3)
                new Color(23, 170, 27),
                new Color(220, 34, 11),
                new Color(23, 67, 172)
        ), new GlyphsVisitor[]{
                new TranslateGlyphsVerticalRandomVisitor(5),
                new RotateGlyphsRandomVisitor(Math.PI / 32),
                new ShearGlyphsRandomVisitor(0.2, 0.2),
                new HorizontalSpaceGlyphsVisitor(5),
                new OverlapGlyphsUsingShapeVisitor(3),
                new TranslateAllToRandomPointVisitor()
        }, new GlyphsDecorator[]{
                // new RandomLinesGlyphsDecorator(0.2, new SingleColorGenerator(new Color(0x306DB3)), 2, 350)
        });

        final int fontSize = Math.round(HEIGHT * 0.85f);
        final FontFamily fontFamily = new RandomFontFamily(fontSize, fontSize, new Font[]{
                new Font("nyala", Font.BOLD, fontSize),
                new Font("Bell MT", Font.PLAIN, fontSize),
                new Font("Credit valley", Font.BOLD, fontSize)
        }, false);

        final Painter painter = new FilterablePainter(backgroundBrush, textBrush, fontFamily, textFilter);
        final RandomTextGenerator gen = new RandomTextGenerator("ABCDEGHJKLMNRSTUWXY235689");

        return new GimpyFactory(WIDTH, HEIGHT, painter, gen, false);
    }
}
